﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Yu.Cms.Common;
using Yu.Cms.IDAL;

namespace Yu.Cms.SQLServerDAL
{
    public class SmallType : ISmallType
    {
        private readonly string TableName = "Y_SmallType";

        #region ISmallType 成员

        /// <summary>
        /// 小类中是否存在大类
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <returns></returns>
        public  bool ExistBigTypeID(int BigTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  "+TableName);
            strSql.Append(" where BigTypeID=@BigTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BigTypeID", SqlDbType.Int,4)};
            parameters[0].Value = BigTypeID;
            return DbHelperMsSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 获取最大数目
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <returns></returns>
        public int GetMaxCount(int BigTypeID)
        {
            return DbHelperMsSQL.GetMaxCount(TableName, " Where BigTypeID=" + BigTypeID);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Yu.Cms.Model.SmallType model)
        {
            int iSort = DbHelperMsSQL.GetMaxID(TableName, "SmallTypeSort", " BigTypeID=" + model.BigTypeID);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + TableName + "(");
            strSql.Append("BigTypeID,SmallTypeName,SmallTypeSort)");
            strSql.Append(" values (");
            strSql.Append("@BigTypeID,@SmallTypeName," + iSort + ")");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BigTypeID", SqlDbType.Int,4),
					new SqlParameter("@SmallTypeName", SqlDbType.NVarChar,50)
					};
            parameters[0].Value = model.BigTypeID;
            parameters[1].Value = model.SmallTypeName;
          
            object obj = DbHelperMsSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return -1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        public int Update(Yu.Cms.Model.SmallType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + TableName + " set ");
            strSql.Append("BigTypeID=@BigTypeID,");
            strSql.Append("SmallTypeName=@SmallTypeName");            
            strSql.Append(" where SmallTypeID=@SmallTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SmallTypeID", SqlDbType.Int,4),
					new SqlParameter("@BigTypeID", SqlDbType.Int,4),
					new SqlParameter("@SmallTypeName", SqlDbType.NVarChar,50)
					};
            parameters[0].Value = model.SmallTypeID;
            parameters[1].Value = model.BigTypeID;
            parameters[2].Value = model.SmallTypeName;            

           return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="SmallTypeID"></param>
        public int Delete(int SmallTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from "+ TableName );
            strSql.Append(" where SmallTypeID=@SmallTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SmallTypeID", SqlDbType.Int,4)};
            parameters[0].Value = SmallTypeID;

            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        /// <param name="SmallTypeID"></param>
        /// <returns></returns>
        public Yu.Cms.Model.SmallType GetModel(int SmallTypeID)
        {
            int RecordCount;
            Yu.Cms.Model.SmallType fam = new Yu.Cms.Model.SmallType();
            QueryParam qp = new QueryParam();
            qp.Where = " Where SmallTypeID=" + SmallTypeID;
            IList<Yu.Cms.Model.SmallType> lst = this.GetList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 通过大类列表获取小类
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <returns></returns>
        public IList<Yu.Cms.Model.SmallType> GetList(int BigTypeID)
        {
            QueryParam qp = new QueryParam();
            qp.Where = " Where BigTypeID=" + BigTypeID;
            int RecordCount;            
            return this.GetList(qp, out RecordCount);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public bool SortSmallType(Yu.Cms.Model.SmallType model, Yu.Enum.Sort sort)
        {
            int SmallTypeSort = model.SmallTypeSort;
            int SmallTypeID = model.SmallTypeID;
            int BigTypeID = model.BigTypeID;
            return DbHelperMsSQL.SwitchSort(TableName, "SmallTypeSort", SmallTypeSort, "SmallTypeID", SmallTypeID, "BigTypeID", BigTypeID, sort);
        }

        #endregion

       

        /// <summary>
        /// 将记录集转为Y_SmallTypeEntity实体类 (Y_SmallType)
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <param name="Fileds">字段名列表</param>
        /// <returns>Y_SmallTypeEntity</returns>
        protected  Yu.Cms.Model.SmallType Populate_SmallType(IDataReader dr, Dictionary<string, string> Fileds)
        {
            Yu.Cms.Model.SmallType nc = new Yu.Cms.Model.SmallType();

            if (Fileds.ContainsKey("SmallTypeID") && !Convert.IsDBNull(dr["SmallTypeID"])) nc.SmallTypeID = Convert.ToInt32(dr["SmallTypeID"]); // 自动ID
            if (Fileds.ContainsKey("BigTypeID") && !Convert.IsDBNull(dr["BigTypeID"])) nc.BigTypeID = Convert.ToInt32(dr["BigTypeID"]); // 大类ID
            if (Fileds.ContainsKey("SmallTypeName") && !Convert.IsDBNull(dr["SmallTypeName"])) nc.SmallTypeName = Convert.ToString(dr["SmallTypeName"]).Trim(); // 小类名称
            if (Fileds.ContainsKey("SmallTypeSort") && !Convert.IsDBNull(dr["SmallTypeSort"])) nc.SmallTypeSort = Convert.ToInt32(dr["SmallTypeSort"]); // 排序
            return nc;
        }

        /// <summary>
        /// 获取iTop条小类
        /// </summary>
        /// <param name="iTop"></param>
        /// <returns></returns>
        public IList<Yu.Cms.Model.SmallType> GetTopList(int iTop)
        {
            QueryParam qp = new QueryParam();
            int RecordCount = 0;
            qp.PageIndex = 1;
            qp.PageSize = iTop;
            return this.GetList(qp, out RecordCount);
        }
        /// <summary>
        /// 返回SmallType实体类的IList对象 (Y_SmallType)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>SmallType实体类的IList对象(Y_SmallType)</returns>
        protected IList<Yu.Cms.Model.SmallType> GetList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = TableName;
            qp.OrderType = 0;
            qp.Orderfld = "SmallTypeSort";  //按SmallTypeSort排序
            
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            else
            {
                qp.ReturnFields += ",";
                qp.ReturnFields += qp.Orderfld;
            }
            DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.SmallType> mypd = new DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.SmallType>(this.Populate_SmallType);
            return DbHelperMsSQL.GetRecordListBySql(mypd, qp, out RecordCount);
        }

        
    }
}
